jQuery(document).ready(function ($) {
    //利用window.location.href得到该网页的url
    var str = window.location.href.split('=')[1];
    var id=parseInt(str);

    //使用jquery获取url及url参数的方法,如果参数中含有中文字符，注意转编码和解码：
    (function (jQuery) {
        // 例如：传入中文参数“华为手机”
        // r为: name=%E5%8D%8E%E4%B8%BA%E6%89%8B%E6%9C%BA,,%E5%8D%8E%E4%B8%BA%E6%89%8B%E6%9C%BA,
        // 解析 ：相当于r为一个字符串数组，r[0]为name,r[1]为=，r[2]就为实际编码的乱码
        jq.getUrlParam = function (name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
            var r = window.location.search.substr(1).match(reg);//匹配目标参数
            if (r != null) return decodeURI(r[2])/*unescape(decodeURI(r[2]))*/; //增加UTF-8解码处理。
            return null;
        }
    })(jQuery);

    //总页数
    var pages;
    //存入初始化查询条件searchMap
    var searchMap = {};
    var categorylevel1Id = jq.getUrlParam('categorylevel1id');
    if(categorylevel1Id == null){
        searchMap['categorylevel1Id'] = '';
    }else{
        searchMap['categorylevel1Id'] = categorylevel1Id;
    }
    var categorylevel2Id = jq.getUrlParam('categorylevel2id');
    if(categorylevel2Id == null){
        searchMap['categorylevel2Id'] = '';
    }else{
        searchMap['categorylevel2Id'] = categorylevel2Id;
    }
    var categorylevel3Id = jq.getUrlParam('categorylevel3id');
    if(categorylevel3Id == null){
        searchMap['categorylevel3Id'] = '';
    }else{
        searchMap['categorylevel3Id'] = categorylevel3Id;
    }
    var name = jq.getUrlParam('name');
    if(name == null){
        searchMap['name'] = '';
    }else{
        searchMap['name'] = name;
    }
    //刚进入按照默认条件排序
    searchMap['condition'] = 'default';
    searchMap['pageNum'] = '1';
    searchMap['price'] = '*-*';



    jq("#ceshi").click(function(){
        alert(name);
    })

    //第一次通过默认数据加载全部数据
    getProductList(searchMap);

    //发送搜索请求，获取符合条件的数据
    function getProductList(searchMap){
        $.ajax({
            url: '/easybuyProduct/getProductListPage',
            // async:false,
            data:{
                'condition':searchMap['condition'],
                'name':searchMap['name'],
                'categoryLevel1Id':searchMap['categorylevel1Id'],
                'categoryLevel2Id':searchMap['categorylevel2Id'],
                'categoryLevel3Id':searchMap['categorylevel3Id'],
                'pageNum':searchMap['pageNum'],
                'price':searchMap['price'],
            },
            success:function(rs){
                pages = rs.pages;
                var productList = rs.productList;
                var highlighting = rs.highlighting;

                $('.cate_list').html('');
                if(productList.length > 0){
                    $('.pages').show();
                    //商品列表
                    $.each(productList,function(index,product){
                        //获取高亮HTML字符串-->productName
                        var productid = product.id;
                        // alert(productid)
                        var a = highlighting[productid];
                        var b = a['name'];   //b相当于获取的高亮map的list集合，为空表示没有高亮
                        var productName = b != null ? b[0] : product.name ;

                        var li = $(
                            '<li>' +
                            '    <div class="img"><a href="Product.html?id='+product.id+'" target="_blank"><img src="files/'+product.fileName+'" width="210" height="185" /></a></div>' +
                            '    <div class="price">' +
                            '        <font>￥<span>'+product.price+'</span></font> &nbsp; '+product.stock+'R' +
                            '    </div>' +
                            '    <div class="name"><a href="#">'+productName+'</a></div>' +
                            '    <div class="carbg">' +
                            '        <a href="javascript:void(0);" name="getInCollection" class="ss" value="'+product.id+'" id="collect" sid="'+product.id+'" onclick="addConn(jq(this))">收藏 </a>' +
                            '        <a href="javascript:void(0);" name="getInCar" class="j_car" value="'+product.id+'" onclick="addBuyCar(jq(this))">加入购物车</a>' +
                            '        <input type="hidden" productId="'+product.id+'">' +
                            '    </div>' +
                            '</li>');
                        $('.cate_list').append(li);
                    });

                    //分页
                    splitPage(searchMap['pageNum'],pages);
                }else{
                    $('.pages').hide();
                    var li = $('<h2>未找到任何商品！</h2>');
                    $('.cate_list').append(li);
                }

                //总数量
                $('.list_t .fr').html('');
                $('.list_t .fr').html('共发现'+rs.numFound+'件');

                //如果有name，则填入搜索框
                if( searchMap['name'] != null && searchMap['name'] != ''){
                    // jq('.s_ipt').val( searchMap['name']);
                    $('.s_ipt').attr("value", searchMap['name']);
                }else{
                    $('.s_ipt').attr("value", searchMap['name']);
                    // jq('.s_ipt').val('');
                }

            }
        })

    };

    //点击翻页
    $('.pages a').click(function(){
        var pageNum = $(this).html();
        var name = $(this).attr('name');
        if(name == 'pre'){
            pageNum = parseInt(searchMap['pageNum']) - 1 <= 0 ? 1 : parseInt(searchMap['pageNum']) - 1;
        }else if(name == 'next'){
            pageNum = parseInt(searchMap['pageNum']) + 1 > pages ? pages : parseInt(searchMap['pageNum']) + 1;
        }
        searchMap['pageNum'] = pageNum;
        getProductList(searchMap);
    });

    //点击搜索按钮
    jq('.s_btn').click(function(){
        var name = jq(this).prev().val().trim();
        if(name == ''){
            // alert('请输入商品名称！');
            // alert("亲，要输入商品名称哦~~");
            jq(this).prev().val('*');
        }else{
            searchMap['name'] = name;
            getProductList(searchMap);
        }
    });

    //价格筛选
    jq('.td_a a').click(function(){
        //去除和添加样式
        jq('.td_a').find('a').removeClass('now');
        jq(this).addClass('now');
        var price = jq(this).attr('price');
        searchMap['price'] = price;
        getProductList(searchMap);
    });

    //点击库存、价格排序
    jq('.list_or a').click(function(){
        //去除和添加样式
        jq('.list_or').find('a').removeClass('now');
        jq(this).addClass('now');

        var a = jq(this).attr('name');
        var condition;
        if('default' == a){
            condition = 'default';
        }else if('price' == a){
            var span = jq(this).children('[name="price_checked"]');
            if(span == ''){
                jq(this).find('[class="i_up"]').attr('name','price_checked');
                condition = 'i_up';
            }else{
                jq(this).find('span').removeAttr('name');
                var span_class = span.attr('class');
                if('i_up' == span_class){
                    jq(this).find('[class="i_down"]').attr('name','price_checked');
                    condition = 'priceDown';
                }else if('i_down' == span_class){
                    jq(this).find('[class="i_up"]').attr('name','price_checked');
                    condition = 'priceUp';
                }
            }
        }else if('stock' == a){
            var span = jq(this).children('[name="stock_checked"]');
            if(span == ''){
                jq(this).find('[class="i_up"]').attr('name','stock_checked');
                condition = 'i_up';
            }else{
                jq(this).find('span').removeAttr('name');
                var span_class = span.attr('class');
                if('i_up' == span_class){
                    jq(this).find('[class="i_down"]').attr('name','stock_checked');
                    condition = 'stockDown';
                }else if('i_down' == span_class){
                    jq(this).find('[class="i_up"]').attr('name','stock_checked');
                    condition = 'stockUp';
                }
            }
        }
        searchMap['condition'] = condition;
        getProductList(searchMap);
    });

})